package com.shsoft;

import com.shsoft.config.SettingConfig;
import com.shsoft.controller.MainController;
import com.shsoft.task.CalendarTask;
import com.shsoft.task.ProcessingComponentsTask;
import com.shsoft.task.SendDataTask;
import com.shsoft.util.SpringUtil;
import gnu.io.SerialPort;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.net.URL;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicBoolean;

public class MonitorApplication extends Application {

    public static AtomicBoolean msgOK = new AtomicBoolean(false);

    public static String msgAnswer;

    public static AtomicBoolean passNormalData = new AtomicBoolean(true);

    public static String projectPath;

    public static MainController mainController;

    public static Scene scene, componentScene;

    public static URL cssUrl;

    public static SerialPort serialport;

    public static Stage componentStage;

    public static SettingConfig settingConfig;

    public static ThreadPoolTaskExecutor taskExecutor;

    public static ConcurrentLinkedQueue<byte[]> normalDataArray = new ConcurrentLinkedQueue<>();

    public static ConcurrentLinkedQueue<byte[]> priorityDataArray = new ConcurrentLinkedQueue<>();

    @Override
    public void start(Stage stage) throws Exception {
        settingConfig = new SettingConfig();
        settingConfig.initConfigs();
        taskExecutor = SpringUtil.getBean("myAsyncThreadPool");
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(loader.getClassLoader().getResource("fxml/main.fxml"));
        Parent root = loader.load();
        mainController = loader.getController();
        MonitorApplication.taskExecutor.execute(new CalendarTask());
        MonitorApplication.taskExecutor.execute(new ProcessingComponentsTask());
        MonitorApplication.taskExecutor.execute(new SendDataTask());
        FXMLLoader componentLoader = new FXMLLoader();
        componentLoader.setLocation(componentLoader.getClassLoader().getResource("fxml/chooseComponent.fxml"));
        VBox componentChoose = componentLoader.load();
        componentScene = new Scene(componentChoose);
        componentScene.getStylesheets().add(cssUrl.toExternalForm());
        componentStage = new Stage();
        componentStage.setScene(componentScene);
        componentStage.getIcons().add(new Image("image/logo.png"));
        componentStage.setTitle(" 选择组件");
        componentStage.setResizable(false);
        componentStage.initOwner(stage);
        componentStage.initModality(Modality.APPLICATION_MODAL);
        scene = new Scene(root, 500, 650);
        scene.getStylesheets().add(cssUrl.toExternalForm());
        stage.getIcons().add(new Image("image/logo.png"));
        stage.setScene(scene);
        stage.setTitle("CleverMonitor 上位机");
        stage.setResizable(false);
        stage.initStyle(StageStyle.TRANSPARENT);
        stage.show();
    }

}
